﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace zp.mk.EfA
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        bool bSaved = true;
        string sFilename = String.Empty;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            saveFile();
        }

        private void txtText_TextChanged(object sender, TextChangedEventArgs e)
        {
            bSaved = false;
        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            bool saved = false;
            MessageBoxResult mbr = MessageBoxResult.No;
            if (!bSaved)
            {
                mbr = MessageBox.Show("Möchten Sie die Datei speichern?", "Speichern", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (mbr == MessageBoxResult.Yes)
                {
                    saved = saveFile();
                }
            }
            if (bSaved || mbr == MessageBoxResult.No)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Textdateien|*.txt";
                if ((bool)ofd.ShowDialog())
                {
                    StreamReader sr = new StreamReader(ofd.FileName);

                    txtText.Text = sr.ReadToEnd();

                    this.Title = "EfA - " + ofd.SafeFileName;

                    bSaved = true;
                    sFilename = ofd.SafeFileName;
                    sr.Close();
                }
            }
        }

        private bool saveFile()
        {

            string sText = txtText.Text;

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Textdateien|*.txt";

            if (sFilename != String.Empty)
            {
                sfd.FileName = sFilename;
            }

            if ((bool)sfd.ShowDialog())
            {

                StreamWriter sw = new StreamWriter(sfd.FileName);
                sw.Write(sText);
                bSaved = true;
                sFilename = sfd.SafeFileName;
                this.Title = "EfA - " + sFilename;

                sw.Close();
            }

            return bSaved;
        }

        private void Window_Closed_1(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MessageBoxResult mbr = MessageBoxResult.No;
            if (!bSaved)
            {
                mbr = MessageBox.Show("Möchten Sie die Datei speichern?", "Speichern", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (mbr == MessageBoxResult.Yes)
                {
                    saveFile();
                }
            }
        }
    }
}